﻿using System;
using System.Diagnostics.Contracts;
#if NETFX_CORE
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml;
#else
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
#endif

namespace DaveSexton.Labs
{
	public sealed class InverseBooleanToVisibilityConverter : IValueConverter
	{
		private readonly BooleanToVisibilityConverter converter = new BooleanToVisibilityConverter();

		[ContractInvariantMethod]
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
		private void ObjectInvariant()
		{
			Contract.Invariant(converter != null);
		}

#if NETFX_CORE
		public object Convert(object value, Type targetType, object parameter, string language)
#else
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
#endif
		{
#if NETFX_CORE
			var result = converter.Convert(value, targetType, parameter, language);
#else
			var result = converter.Convert(value, targetType, parameter, culture);
#endif

			if (result is Visibility)
			{
				return (Visibility) result == Visibility.Visible
					? Visibility.Collapsed
					: Visibility.Visible;
			}
			else
			{
				return result;
			}
		}

#if NETFX_CORE
		public object ConvertBack(object value, Type targetType, object parameter, string language)
#else
		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
#endif
		{
#if NETFX_CORE
			var result = converter.ConvertBack(value, targetType, parameter, language);
#else
			var result = converter.ConvertBack(value, targetType, parameter, culture);
#endif

			if (result is bool)
			{
				return !(bool) result;
			}
			else
			{
				return result;
			}
		}
	}
}